package com.news.web;

import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;

import com.news.entity.Emp;
import com.news.service.EmpService;

@Controller
@RequestMapping(value="/emp")
public class EmpHandler {
	@Resource
	private EmpService empService;//调用业务层
	
	@RequestMapping("/tosave")
	public String toSave(Map<String, Emp> map){
		map.put("emp", new Emp());
		return "emp_save";
	}
	
	@RequestMapping("/save")
	public String save(@Valid Emp emp,Errors errors){
		if(errors.getErrorCount()>0){
			for(FieldError fieldError : errors.getFieldErrors()){
				System.out.println("出错了:"+fieldError.getField()+"-->"+fieldError.getDefaultMessage());
			}
			return "emp_save";
		}
		System.out.println("emp=========="+emp);
		empService.save(emp);
		System.out.println("xxxxxxxxxxxxxxxxxxxxxxx");
		return "success";
	}

	public EmpService getEmpService() {
		return empService;
	}

	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}
	
}
